package dataset.generation;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.List;

import org.apache.log4j.PropertyConfigurator;

public class splitGenerator {

	private static Hashtable<String,PrintStream> printStreams = new Hashtable<String, PrintStream>();

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		
		double factor = 1; //how many times the falses exceed the positives
		
		JSREFileCreator.loadFileForCombinations(new File("/home/pjbarrio/Dataset/SGML-ACE/COMBINATIONS"));
		
		for (int dataset = 0; dataset < JSREFileCreator.Datasets.length; dataset++) {
			
			String[] firstArgs = JSREFileCreator.getFirstArgument(JSREFileCreator.Datasets[dataset]);
			
			for (int first = 0; first < firstArgs.length; first++) {
				
				String[] secondArgs = JSREFileCreator.getSecondArgument(JSREFileCreator.Datasets[dataset],firstArgs[first]);
				
				for (int second = 0; second < secondArgs.length; second++) {
					
					int[] split = JSREFileCreator.getSplits(JSREFileCreator.Datasets[dataset]);
					
					for (int i = 0; i < split.length; i++) {
				
						System.gc();
						
						System.err.println(JSREFileCreator.Datasets[dataset] + " - Split: " + split[i] + " - " + firstArgs[first] + " - " + secondArgs[second]);
						
						String generatedTrueFile = JSREFileCreator.getFormattedFile(firstArgs[first], secondArgs[second], JSREFileCreator.Datasets[dataset], split[i], true, "train",true);
								
						String generatedFalseFile = JSREFileCreator.getFormattedFile(firstArgs[first], secondArgs[second], JSREFileCreator.Datasets[dataset], split[i], false, "train",true);;
						
						List<String> trues = readLines(generatedTrueFile);
						
						List<String> falses = readLines(generatedFalseFile);
						
						if (trues.size() == 0 || falses.size() == 0)
							continue;
						
						int numberOfSplits = getNumberOfSplits(falses.size(),trues.size(),factor);
						
						int index = 0;
						
						System.out.println(trues.size() + " - " + falses.size());
						
						for (String string : falses) {
							
								PrintStream ps = getPrintStream(JSREFileCreator.getFormattedFile(firstArgs[first], secondArgs[second], JSREFileCreator.Datasets[dataset], split[i], true, "train",false),getRound(index,numberOfSplits));
							
								ps.println(string);
								
								index++;
								
						}
						
						for(Enumeration<PrintStream> e = printStreams.elements(); e.hasMoreElements();){
							
							PrintStream ps = e.nextElement();
							
							for (String string : trues) {
								
								ps.println(string);
								
							}
							
							ps.close();
							
						}
						
					}

					
				}
				
			}
			
			
		}

		

	}

	private static int getNumberOfSplits(int falses, int trues, double factor) {
		
		if (factor == 0){
			return 1;
		}
		
		int div = (int)Math.floor(falses/(trues/factor));
		
		if (div == 0)
			div = 1;
		
		return div;
		
	}

	private static int getRound(int index, int numberOfSplits) {
		
		return index % numberOfSplits;
		
	}

	private static PrintStream getPrintStream(String generatedPrefix,
			int split) throws FileNotFoundException {
		
		String id = JSREFileCreator.getSplittedFormatedFile(generatedPrefix,split);
		
		PrintStream ret = printStreams .get(id);
		
		if (ret == null){
			
			ret = new PrintStream(id);
			
			printStreams.put(id,ret);
		
		}
		
		return ret;

		
	}

	

	private static List<String> readLines(String generatedFile) throws IOException {
		
		ArrayList<String> ret = new ArrayList<String>();
		
		File file = new File(generatedFile);
		
		if (!file.exists()){
			return ret;
		}
		
		BufferedReader br = new BufferedReader(new FileReader(file));
		
		String line = br.readLine();
		
		while (line != null){
			
			ret.add(line);
			
			line  = br.readLine();
		}
		
		br.close();
		
		return ret;
	}

}
